#include "addressBook.h"

// displayMenu function definition
void displayMenu() {
    cout << "1. Add contact person" << endl;
    cout << "2. Search contact person" << endl;
    cout << "3. Delete contact person" << endl;
    cout << "4. Update contact person" << endl;
    cout << "5. Display all contact person" << endl;
    cout << "6. Delete all contact person" << endl;
    cout << "7. Exit" << endl;
    return;
}

// addContactPerson function definition
void addContactPerson(ContactPersons *p) {
    if(p->n == MAX) {
        cout << "Contact person list is full" << endl;
        return;
    }
    cout << "Add contact person" << endl;
    cout << "-------------------" << endl;
    cout << "Enter name: ";
    string name;
    cin >> name;
    cout << "Enter sex: ";
    string sex;
    cin >> sex;
    cout << "Enter age: ";
    int age;
    cin >> age;
    cout << "Enter phone: ";
    string phone;
    cin >> phone;
    cout << "Enter address: ";
    string address;
    cin >> address;
    cout << endl;
    p->contactPerson[p->n].name = name;
    p->contactPerson[p->n].sex = sex;
    p->contactPerson[p->n].age = age;
    p->contactPerson[p->n].phone = phone;
    p->contactPerson[p->n].address = address;
    p->n++;
    cout << "Contact person added" << endl;
    return;
}

// searchContactPerson by name function definition
void searchContactPerson(ContactPersons *p) {
    cout << "Search contact person" << endl;
    cout << "----------------------" << endl;
    cout << "Enter name: ";
    string name;
    cin >> name;
    cout << endl;
    cout << "Contact person details:" << endl;
    for (int i = 0; i < p->n; i++) {
        if (p->contactPerson[i].name == name) {
            cout << "Name: " << p->contactPerson[i].name << endl;
            cout << "Sex: " << p->contactPerson[i].sex << endl;
            cout << "Age: " << p->contactPerson[i].age << endl;
            cout << "Phone: " << p->contactPerson[i].phone << endl;
            cout << "Address: " << p->contactPerson[i].address << endl;
            return;
        }
    }
    cout << "Contact person not found" << endl;
    return;
}

// deleteContactPerson by name function definition
void deleteContactPerson(ContactPersons *p) {
    cout << "Delete contact person" << endl;
    cout << "----------------------" << endl;
    cout << "Enter name: ";
    string name;
    cin >> name;
    cout << endl;
    for (int i = 0; i < p->n; i++) {
        if (p->contactPerson[i].name == name) {
            for (int j = i; j < p->n - 1; j++) {
                p->contactPerson[j] = p->contactPerson[j + 1];
            }
            p->n--;
            cout << "Contact person deleted" << endl;
            return;
        }
    }
    cout << "Contact person not found" << endl;
    return;
}

// updateContactPerson by name function definition
void updateContactPerson(ContactPersons *p) {
    cout << "Update contact person" << endl;
    cout << "----------------------" << endl;
    cout << "Enter name: ";
    string name;
    cin >> name;
    cout << endl;
    for (int i = 0; i < p->n; i++) {
        if (p->contactPerson[i].name == name) {
            cout << "Enter new name: ";
            string name;
            cin >> name;
            cout << "Enter sex: ";
            string sex;
            cin >> sex;
            cout << "Enter age: ";
            int age;
            cin >> age;
            cout << "Enter phone: ";
            string phone;
            cin >> phone;
            cout << "Enter address: ";
            string address;
            cin >> address;
            cout << endl;
            p->contactPerson[i].name = name;
            p->contactPerson[i].sex = sex;
            p->contactPerson[i].age = age;
            p->contactPerson[i].phone = phone;
            p->contactPerson[i].address = address;
            cout << "Contact person updated" << endl;
            return;
        }
    }
    cout << "Contact person not found" << endl;
    return;
}

// displayAllContactPerson function definition
void displayAllContactPerson(ContactPersons *p) {
    if(p->n == 0) {
        cout << "No contact person" << endl;
        return;
    }
    cout << "Display all contact person" << endl;
    cout << "--------------------------" << endl;
    for (int i = 0; i < p->n; i++) {
        cout << "Name: " << p->contactPerson[i].name << endl;
        cout << "Sex: " << p->contactPerson[i].sex << endl;
        cout << "Age: " << p->contactPerson[i].age << endl;
        cout << "Phone: " << p->contactPerson[i].phone << endl;
        cout << "Address: " << p->contactPerson[i].address << endl;
        cout << endl;
    }
    return;
}

// deleteAllContactPerson function definition
void deleteAllContactPerson(ContactPersons *p) {
    cout << "Delete all contact person" << endl;
    cout << "--------------------------" << endl;
    p->n = 0;
    cout << "All contact persons deleted" << endl;
    return;
}